﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Repository.Interface.Ms;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class AlgorithmRepository : BaseRepository<MsAlgorithmParamTemplate>, IAlgorithmRepository
    {
        /// <summary>
        /// 获取项目所有的算法模板
        /// </summary>
        /// <param name="projectId">项目id</param>
        /// <returns></returns>
        public async Task<List<MsAlgorithmParamTemplate>> GetListByProjectId(Guid projectId)
        {
            return await Db.MsProjectAlgorithms.Join(Db.MsAlgorithmParamTemplates,
                                                        x => x.AlgorithmId,
                                                        y => y.AlgorithmParamTemplateGuid,
                                                        (x, y) => new { ProJectId = x.ProjectId, Algorithm = y })
                                                .Where(x => x.ProJectId == projectId && !x.Algorithm.IsDeleted)
                                                .Select(x => new MsAlgorithmParamTemplate
                                                {
                                                    AlgorithmParamTemplateGuid = x.Algorithm.AlgorithmParamTemplateGuid,
                                                    TemplateName = x.Algorithm.TemplateName,
                                                    TemplateType = x.Algorithm.TemplateType,
                                                    CreatedBy = x.Algorithm.CreatedBy,
                                                    CreatedDate = x.Algorithm.CreatedDate,
                                                    LastUpdatedBy = x.Algorithm.LastUpdatedBy,
                                                    LastUpdatedDate = x.Algorithm.LastUpdatedDate,
                                                }).ToListAsync().ConfigureAwait(false);
        }

        /// <summary>
        /// 获取算法模板配置项详情
        /// </summary>
        /// <param name="algorithmParamTemplateId">算法模板Id</param>
        /// <returns></returns>
        public async Task<List<MsAlgoItemDto>> GetAlgorithmParamTemplateItems(Guid algorithmParamTemplateId)
        {
            return await Db.MsAlgorithmParamTemplateItems.Where(x => x.AlgorithmParamTemplateGuid == algorithmParamTemplateId && !x.AlgorithmParamTemplate.IsDeleted)
                .Select(x => new MsAlgoItemDto
                {
                    AlgorithmParamTemplateGuid = x.AlgorithmParamTemplateGuid,
                    TemplateName = x.AlgorithmParamTemplate.TemplateName,
                    MeasureType = x.MeasureType,
                    AlgorithmParamItemGuid = x.AlgorithmParamItemGuid,
                    ItemGroup = x.ItemGroup,
                    ItemGroupImagePath = x.ItemGroupImagePath,
                    ItemKey = x.ItemKey,
                    ItemName = x.ItemName,
                    ItemType = x.ItemType,
                    ItemValue = x.ItemValue,
                    ItemValueRange = x.ItemValueRange,
                }).ToListAsync().ConfigureAwait(false);
        }

        public Guid DeleteAlgorithmTemplate(Guid algorithmParamTemplateId)
        {
            RemoveById(algorithmParamTemplateId);
            var msProjectAlgorithmEntity = Db.MsProjectAlgorithms.FirstOrDefault(x => x.AlgorithmId == algorithmParamTemplateId);
            return msProjectAlgorithmEntity.ProjectId;
        }

        public async Task<List<MsProjectAlgorithm>> GetMsProjectAlgorithmsByProjIds(Guid[] projIds)
        {
            return await Db.MsProjectAlgorithms.Where(x => projIds.Contains(x.ProjectId) && !x.IsDeleted).ToListAsync().ConfigureAwait(false);
        }

        public async Task<List<MsAlgorithmParamTemplate>> GetMsAlgorithmParamTemplatesByProjIds(Guid[] projIds)
        {
            return await Db.MsAlgorithmParamTemplates.Join(Db.MsProjectAlgorithms,
                                                        x => x.AlgorithmParamTemplateGuid,
                                                        y => y.AlgorithmId,
                                                        (x, y) => new { ProjectId = y.ProjectId, AlgorithmParamTemplate = x })
                                                    .Where(x => projIds.Contains(x.ProjectId))
                                                    .Select(x => x.AlgorithmParamTemplate)
                                                    .ToListAsync()
                                                    .ConfigureAwait(false);
        }

        public async Task<List<MsAlgorithmParamTemplateItem>> GetAlgorithmParamTemplateItemsByProjIds(Guid[] projIds)
        {
            return await Db.MsAlgorithmParamTemplateItems.Join(Db.MsProjectAlgorithms,
                                                                x => x.AlgorithmParamTemplateGuid,
                                                                y => y.AlgorithmId,
                                                                (x, y) => new { ProjectId = y.ProjectId, AlgorithmParamTemplateItem = x })
                                                        .Where(x => projIds.Contains(x.ProjectId))
                                                        .Select(x => x.AlgorithmParamTemplateItem)
                                                        .ToListAsync()
                                                        .ConfigureAwait(false);
        }
    }
}